// @ts-nocheck
const pool = require('../mysql/mysql.js')
const { validateFields } = require('../public/util.js')
const { successMsg, errorMsg } = require('../public/constant.js')
const {executeTransaction}=require('../mysql/public.js')
const express = require('express');
const router = express.Router();
const multer = require('multer');
const upload = multer();

const tableName='room_type'
// 查询数据

router.get('/List', (req, res) => {
    pool.query(`SELECT * FROM ${tableName} Where is_delete=0`, (err, result) => {
        if (err){
            return res.status(500).send(errorMsg('数据库错误'));
        }
        var r = [...result]
        res.status(200).send(successMsg(r));
    });
});

// 添加
router.post('/Add', upload.none(), validateFields([
    { field: 'roomTypeName', noEmpty: true, validType: 'string' },
    { field: 'roomTypePrice', noEmpty: true, validType: 'number' },
    { field: 'bedNum', noEmpty: true, validType: 'number' },
    { field: 'description', noEmpty: true, validType: 'string' },
]), (req, res) => {
    const { roomTypeName,roomTypePrice,bedNum,description } = req.body;
    executeTransaction({pool,res,queries:[
        {
            sql:`select * from ${tableName} where room_type_name=? and is_delete=0`,
            params:[roomTypeName],
            stopFlag:(result)=>result.length,
            stopEvent:()=>{res.status(200).send(errorMsg('名称重复'))},
        },
        {
            sql:`INSERT INTO ${tableName} (room_type_name,room_type_price,bed_num,description) VALUES (?,?,?,?)`,
            params:[roomTypeName, roomTypePrice,bedNum,description],
            successEvent:()=>{res.status(200).send(successMsg('添加成功'))}
        }
    ]})
    
});
// 删除
router.get('/Delete', (req, res) => {
    const { roomTypeId } = req.query
    if (!roomTypeId) {
        return res.status(200).send(errorMsg('缺少参数roomTypeId'));
    }
    executeTransaction({pool,res,queries:[
        {
            sql:`SELECT * FROM ${tableName} WHERE room_type_id=? and is_delete=0`,
            params:[roomTypeId],
            stopFlag:(result)=>!result.length,
            stopEvent:()=>{res.status(200).send(errorMsg('无效id'))},
        },
        {
            sql:`UPDATE ${tableName} SET is_delete=1 WHERE room_type_id=?`,
            params:[roomTypeId],
            successEvent:()=>{res.status(200).send(successMsg('操作成功'));}
        }
    ]})
});
// 获取某一个房型
router.post('/GetOne', upload.none(), validateFields([
    { field: 'roomTypeId', noEmpty: true, validType: 'number' },
]), (req, res) => {
    const { roomTypeId } = req.body
    const sql = `SELECT * FROM ${tableName} WHERE room_type_id=? and is_delete=0`
    pool.query(sql, [roomTypeId], (err, result) => {
        if (err){
            return res.status(500).send(errorMsg('数据库错误'));
        }
        var r = [...result]
        res.status(200).send(successMsg(r))
    })
});
// 修改
router.post('/Edit', upload.none(), validateFields([
    { field: 'roomTypeId', noEmpty: true, validType: 'number' },
    { field: 'roomTypeName', noEmpty: true, validType: 'string' },
    { field: 'roomTypePrice', noEmpty: true, validType: 'number' },
    { field: 'bedNum', noEmpty: true, validType: 'number' },
    { field: 'description', noEmpty: true, validType: 'number' },
]), (req, res) => {
    const { roomTypeId,roomTypeName,roomTypePrice,bedNum,description } = req.body;
    executeTransaction({pool,res,queries:[
        {
            sql:`SELECT * FROM ${tableName} WHERE room_type_id=? and is_delete=0`,
            params:[roomTypeId],
            stopFlag:(result)=>!result.length,
            stopEvent:()=>{res.status(200).send(errorMsg('没有该房型'))}
        },
        {
            sql:`SELECT * FROM ${tableName} WHERE room_type_name=? and is_delete=0 and room_type_id not in (?)`,
            params:[roomTypeName,roomTypeId],
            stopFlag:(result)=>result.length,
            stopEvent:()=>{res.status(200).send(errorMsg('名称重复'))},
        },
        {
            sql:`UPDATE ${tableName} SET room_type_name=?, room_type_price=?, bed_num=?, description=? WHERE room_type_id=?`,
            params:[roomTypeName,roomTypePrice,bedNum,description,roomTypeId],
            successEvent:()=>{res.status(200).send(successMsg('修改成功'));}
        }
    ]})
    
});

module.exports = router;